/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.sync;

import java.util.List;

import v1.sync.web.bifang.han.SyncRequestDocument;
import v1.sync.web.bifang.han.SyncRequestDocument.SyncRequest.Clear.Type;
import han.bifang.business.bashe.BasheClient;
import han.bifang.business.dao.BusBindingDao;
import han.bifang.business.dao.ServiceBusDao;
import han.bifang.common.entity.EntityConstants;
import han.bifang.common.entity.SoaHttpBinding;
import han.bifang.common.entity.SoaServiceBus;
import han.bifang.common.exception.BifangException;

/**
 * @Author kent
 */
public class SyncUtils {

	public final static String SERVICE = "service";

	public final static String MAPPING_SERVICE = "mappingService";
	public final static String SERVICE_BUS = "serviceBus";
	public final static String SECURITY = "security";

	public static void sync(String type, String id) throws BifangException {
		SyncRequestDocument doc = SyncRequestDocument.Factory.newInstance();
		doc.addNewSyncRequest().addNewClear();

		if (type.equals(SERVICE))
			doc.getSyncRequest().getClear().setType(Type.SERVICE);

		else if (type.equals(MAPPING_SERVICE)) {
			doc.getSyncRequest().getClear().setType(Type.MAPPING_SERVICE);
		} else if (type.equals(SERVICE_BUS)) {
			doc.getSyncRequest().getClear().setType(Type.SERVICE_BUS);
		} else if (type.equals(SECURITY)) {
			doc.getSyncRequest().getClear().setType(Type.SECURITY);
		}

		doc.getSyncRequest().getClear().setKey(id);

		List<SoaServiceBus> bus = ServiceBusDao.getLocalServiceBuses();
		for (SoaServiceBus b : bus) {
			SoaHttpBinding hb = (SoaHttpBinding) BusBindingDao.getBusBinding(b
					.getCode(), EntityConstants.BUS_BINDING_SYNC,
					EntityConstants.BINDING_HTTP);
			try{
				BasheClient.sync(doc, hb.getUrl(), 60);
			}catch (Exception e) {
				e.printStackTrace();
			}
		}

		// 

	}

}
